package com.lc.tgxm.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;

import com.lc.tgxm.R;
import com.lc.tgxm.dialog.ClearDialog;
import com.zcx.helper.util.UtilToast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 通用工具类
 * Created by Lei on 2016/7/4.
 */
public class Utils {

    // 校验手机号
    public static boolean checkMobile(String mobile) {
        try {
            return mobile.matches("^1\\d{10}$");
        } catch (Exception var2) {
            return false;
        }
    }

    // 手机号校验
    public static boolean checkPhonenumber(String pnum) {
        if (TextUtils.isEmpty(pnum)) {
            UtilToast.show(R.string.to_phonenull);
        } else if (!Utils.checkMobile(pnum)) {
            UtilToast.show(R.string.to_phoneillegal);
        } else {
            return true;
        }
        return false;
    }

    public static String conceal(int before, int later, String string) {
        if (before + later < string.length()) {
            int size = string.length();
            char[] temp = new char[size];
            for (int i = 0; i < size; ++i) {
                temp[i] = i > before - 1 && i < string.length() - later ? 42 : string.charAt(i);
            }
            return new String(temp);
        }
        return null;
    }

    //根据Wifi信息获取本地Mac
    public static String getLocalMacAddressFromWifiInfo(Context context) {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getMacAddress();
    }

    public static String getMac() {
        String macSerial = null;
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);


            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return macSerial;
    }

    public static String getWifiMac(Context context) {
        String str = "";
        WifiManager mWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (mWifi.isWifiEnabled()) {
            WifiInfo wifiInfo = mWifi.getConnectionInfo();
            String netName = wifiInfo.getSSID(); //获取被连接网络的名称
            String netMac = wifiInfo.getBSSID(); //获取被连接网络的mac地址
            String localMac = wifiInfo.getMacAddress();// 获得本机的MAC地址
            Log.d("MainActivity", "---netName:" + netName);   //---netName:HUAWEI MediaPad
            Log.d("MainActivity", "---netMac:" + netMac);     //---netMac:78:f5:fd:ae:b9:97
            Log.d("MainActivity", "---localMac:" + localMac); //---localMac:BC:76:70:9F:56:BD
            str = netName + "\n" + netMac + "\n" + localMac;
        }
        return str;
    }

    /**
     * 毫秒数转换成小时分钟秒
     *
     * @param ms 毫秒数
     * @return 00小时00分钟00秒
     */
    public static String ms2HourMinSecs(long ms) {
        StringBuilder sb = new StringBuilder();
        long days = ms / (1000 * 60 * 60 * 24);
        long hours = (ms % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (ms % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (ms % (1000 * 60)) / 1000;
        if (days > 0) {
            sb.append(days).append("天");
        }
        if (hours > 0) {
            sb.append(hours).append("小时");
        }
        if (minutes > 0) {
            sb.append(minutes).append("分钟");
        }
        if (seconds > 0) {
            sb.append(seconds).append("秒");
        }
        return sb.toString();
    }

    public static String ms2HourMinSecsColon(long ms) {
        StringBuilder sb = new StringBuilder();
        long days = ms / (1000 * 60 * 60 * 24);
        long hours = (ms % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (ms % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (ms % (1000 * 60)) / 1000;
        if (days > 0) {
            sb.append(days).append("天");
        }
        if (hours >= 0) {
            String h = hours > 9 ? "" + hours : "0" + hours;
            sb.append(h).append(":");
        }
        if (minutes >= 0) {
            String m = minutes > 9 ? "" + minutes : "0" + minutes;
            sb.append(m).append(":");
        }
        if (seconds >= 0) {
            String s = seconds > 9 ? "" + seconds : "0" + seconds;
            sb.append(s);
        }
        return sb.toString();
    }

    /**
     * 将日期转为毫秒数
     * 20150918转为毫秒数
     *
     * @return 毫秒数
     */
    public static long str2longTime(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss", Locale.CHINA);
        Date date = new Date();
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 获取当前时间
     *
     * @return 格式化好的时间
     */
    public static String getCurrentDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss", Locale.CHINA);
        return sdf.format(System.currentTimeMillis());
    }

    /**
     * 获取当前时间
     *
     * @return 格式化好的时间
     */
    public static String getCurrentDateTime2() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.CHINA);
        return sdf.format(System.currentTimeMillis());
    }

    /**
     * double转String,保留小数点后两位
     * 使用0.00不足位补0，#.##仅保留有效位
     *
     * @param num 数据源
     * @return 格式化后的数
     */
    public static String doubleToString(double num) {
        return new DecimalFormat("#.##").format(num);
    }

    public static String doubleToString0(double num) {
        return new DecimalFormat("0.00").format(num);
    }

    /**
     * 从Assets文件夹下读取 文件
     *
     * @param context  Context
     * @param fileName 文件名
     * @return String 字符串
     */
    public static String getStringFromAssets(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 验证包含数字和字母
     *
     * @return true 必须同时包含数字和字母
     */
    public static boolean isLettersAndDigit(String txt) {
        boolean isDigit = false;// 定义一个boolean值，用来表示是否包含数字
        boolean isLetter = false;// 定义一个boolean值，用来表示是否包含字母
        for (int i = 0; i < txt.length(); i++) {
            if (Character.isDigit(txt.charAt(i))) { // 用char包装类中的判断数字的方法判断每一个字符
                isDigit = true;
            }
            if (Character.isLetter(txt.charAt(i))) { // 用char包装类中的判断字母的方法判断每一个字符
                isLetter = true;
            }
        }
        return isDigit && isLetter;
    }

    public static void showDialog(Context context, int resname,
                                  ClearDialog.OnItemClick onItemClick) {
        ClearDialog clearDialog = new ClearDialog(context);
        clearDialog.setTitlename(resname);
        clearDialog.setOnItemClick(onItemClick);
        clearDialog.show();
    }

    /**
     * 隐藏软键盘
     */
    public static void hideSoftInput(Activity activity) {
        InputMethodManager imm =
                (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    /**
     * 利用Html,字体不同颜色 333 helloworld
     *
     * @param text1 前面的普通字体 i am master
     * @param text2 有颜色的字体
     * @param text3 后面的普通字体
     * @param color 字体颜色
     * @return Spanned
     */
    public static Spanned getHtmlTextview(Context context, int color, String text1, String text2, String text3) {
        String ljq = text1 + "<font color='" + ContextCompat.getColor(context, color) + "'>" + text2 + "</font>" + text3;
        return Html.fromHtml(ljq);
    }
}